package com.hilaver.dzmis.exhibition.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.util.List;
import java.util.Locale;

import com.hilaver.dzmis.exhibition.utils.Constants;
import com.hilaver.dzmis.exhibition.hibernate.BaseHibernateDAO;
import com.hilaver.dzmis.exhibition.utils.SimpleObj2XML;

public abstract class AbstractBaseService {

	public final static String COLUMN_ID = "id";
	
	public final static String COLUMN_NAME = "name";
	
	public final static String COLUMN_PHOTO_NAME = "photoName";
	
	public final static String COLUMN_DESCRIPTOIN = "description";
	
	public final static String COLUMN_COMMENT = "comment";
	
	public final static String COLUMN_CUSTOMER_ID = "biFranceCustomerId";
	
	public final static String COLUMN_CUSTOMER_NAME = "biFranceCustomer";
	
	protected BaseHibernateDAO dao;
	
	protected String realFileDir;
	
	protected Locale locale;
	
	protected final static String NO_IMAGE_NAME = "noimage.gif";  

	protected AbstractBaseService() {
		this.dao = new BaseHibernateDAO();
	}

	public String edit(Object obj) throws Exception {
		this.dao.saveOrUpdate(obj);
		return "{success: true}";
	}

	public String delete(String className, int id) throws Exception {
		this.dao.delete(className, id);
		return "success";
	}

	protected String get(String className, int id) throws Exception {
		Object obj = this.dao.get(className, id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	protected Object getObj(String className, int id) throws Exception{
		return this.dao.get(className, id);
	}

	protected String getAllPagination(String className, int page, String sort,
			String order) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(className, offset,
				Constants.PAGE_SIZE, sort, order);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(className));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	protected String getAll(String className, String sort,
			String order) throws Exception{
		List objList = this.dao.getAll(className, sort, order);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(objList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	protected String getAllPagination(String className, int page, String sort,
			String order, String[] filter) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(className, offset,
				Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(className, filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	protected static void writeFile(String filePath, Blob fileBlob){
		if(fileBlob == null){
			return;
		}
		File file = new File(filePath);
		if(file.exists()){
			return;
		}
		InputStream is = null;
		FileOutputStream fos = null;
		try {
			is = fileBlob.getBinaryStream();
			fos = new FileOutputStream(filePath);
			byte[] buf = new byte[102400];
			   
			int len;
			while((len = is.read(buf))!= -1){
			    fos.write(buf,0,len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(fos != null){
					fos.close();
				}
				if(is != null){
					is.close();
				}
			} catch (IOException e) {
			}
		}
	}

	public void setRealFileDir(String realFileDir) {
		this.realFileDir = realFileDir;
	}
	

	public void setLocale(Locale locale) {
		this.locale = locale;
	}
	
	public void evic(Object obj){
		this.dao.getSession().evict(obj);
	}
}
